﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System.Collections.Generic;
using System.Linq;
using DotNet.Collections;
using DotNet.Edu.Entity;
using DotNet.Extensions;
using DotNet.Utility;

namespace DotNet.Edu.Service
{
    /// <summary>
    /// 安全两类人培训费支付订单服务
    /// </summary>
    public class AqpxPayOrderService
    {
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="entity">实体</param>
        public BoolMessage Create(AqpxPayOrder entity)
        {
            var repos = new AqpxRepository<AqpxPayOrder>();
            repos.Insert(entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="entity">实体</param>
        public BoolMessage Update(AqpxPayOrder entity)
        {
            var repos = new AqpxRepository<AqpxPayOrder>();
            repos.Update(entity);
            return BoolMessage.True;
        }

        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="isCreate">是否新增</param>
        public BoolMessage Save(AqpxPayOrder entity, bool isCreate)
        {
            return isCreate ? Create(entity) : Update(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="ids">主键数组</param>
        public BoolMessage Delete(string[] ids)
        {
            var repos = new AqpxRepository<AqpxPayOrder>();
            repos.Delete(ids);
            return BoolMessage.True;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id">主键</param>
        public AqpxPayOrder Get(string id)
        {
            return new AqpxRepository<AqpxPayOrder>().Get(id);
        }
        
        /// <summary>
        /// 获取启用的对象集合(已排序)
        /// </summary>
        public List<AqpxPayOrder> GetList()
        {
            var repos = new AqpxRepository<AqpxPayOrder>();
            var query = repos.SQL.OrderByDesc(p => p.create_date_time);
            return repos.Query(query).ToList();
        }
        
        /// <summary>
        /// 获取对象分页集合
        /// </summary>
        public PageList<AqpxPayOrder> GetPageList(PaginationCondition pageCondition,
            string name, string trade_no,string trade_state, string order_type, string start_date, string end_date)
        {
            pageCondition.SetDefaultOrder(nameof(AqpxPayOrder.create_date_time));
            var repos = new AqpxRepository<AqpxPayOrder>();
            var query = repos.PageQuery(pageCondition);
            if (name.IsNotEmpty())
            {
                name = name.Trim();
                query.Where(p => p.name.Contains(name) || p.phone.Contains(name) || p.id_card.Contains(name));
            }
            if (trade_no.IsNotEmpty())
            {
                trade_no = trade_no.Trim();
                query.Where(p => p.transaction_id.Contains(trade_no) || p.out_trade_no.Contains(trade_no));
            }
            if (trade_state.IsNotEmpty())
            {
                trade_state = trade_state.Trim();
                query.Where(p => p.trade_state==trade_state);
            }

            if (order_type.IsNotEmpty())
            {
                order_type = order_type.Trim();
                if(order_type=="NULL")
                {
                    query.Where(p => p.order_type ==null);
                }
                else
                    query.Where(p => p.order_type == order_type);
            }
            
            if (start_date.IsNotEmpty())
            {
                var startDate = start_date.ToDateTime();
                query.Where(p => p.create_date_time >= startDate);
            }
            if (end_date.IsNotEmpty())
            {
                var endDate = end_date.ToDateTime().AddDays(1);
                query.Where(p => p.create_date_time <= endDate);
            }
            return repos.Page(query);
        }
    }
}
